package untiring.beans;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import untiring.interfaces.GalleryDAO;
import untiring.models.GalleryVO;

@Repository("galleryDAO")
public class GalleryDAOImpl implements GalleryDAO{
	
	@Autowired
	private SqlSession sqlSession;
	
	@Override
	public boolean insertGallery(GalleryVO g) {
		boolean result = false;
		if(sqlSession.insert("galleryMapper.insertGallery",processImage(g))>0 )result = true;
		return result;
	}
	
	public Map<String,Object> processImage(GalleryVO g){
		Map<String,Object> sendData = new HashMap<>();
		sendData.put("gvo", g); //title, uid, contents ,uuid
		int cnt = 1;
		for(MultipartFile mf:g.getImg()){
			String fname = "";
			if(mf.getSize()>0)
				fname = mf.getOriginalFilename();
			sendData.put("img"+(cnt++), fname);
		} //for
		return sendData;
	}
	
	
	
	@Override
	public List<GalleryVO> selectGallery() {
		return sqlSession.selectList("galleryMapper.selectGallery");
	}

	@Override
	public GalleryVO selectOneGallery(String gno) {
		return sqlSession.selectOne("galleryMapper.viewGallery", gno);
	}

	@Override
	public boolean updateGallery(GalleryVO g) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteGallery(String gno) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public GalleryVO selectImage(GalleryVO g) {
		// TODO Auto-generated method stub
		return sqlSession.selectOne("galleryMapper.viewImage", g);
	}

}
